package com.school.generator.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: zhangyaoting
 * @date: 2019/07/03 22:00
 * @version: V1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class TableItem {
    /** 前缀符号 **/
    private String prevChar;
    /** 表名 **/
    private String tableName;
    /** 实体名(首字母小写) **/
    private String entityNameLower;
    /** 实体名(首字母大写) **/
    private String entityName;
    /** 主键 **/
    private ColumnItem key;
    /** 所有列 **/
    private List<ColumnItem> columns;
    /** 非主键列 **/
    private List<ColumnItem> normalColumns;
    /** 依赖名集合  **/
    private List importNames;
    /** 查询的列集合 **/
    private String columnList;
    /** 基础包名 **/
    private String basePackage;
    /** 基础包名文件夹 **/
    private String packagePath;
    /** 项目路径 **/
    private String basePath;
    /** 作者 **/
    private String userName;
    /** 邮箱 **/
    private String userEmail;
    /** 创建时间 **/
    private String createDate;

    public void init(){
        prevChar = "#";
        packagePath = basePackage.replaceAll("\\.", "/");
        if(StringUtils.isEmpty(entityName)){
            entityNameLower = ColumnItem.camelCaseName(tableName.replaceFirst("s_",""));
            entityName = toUpperCaseFirstOne(entityNameLower);
        } else {
            entityNameLower = toLowerCaseFirstOne(entityName);
        }
        columnList = columns.stream().map(c -> "`" + c.getColumnName() + "`").collect(Collectors.joining(","));

        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        createDate = df.format(Calendar.getInstance().getTime());

        String primary = "PRI";
        normalColumns = new ArrayList<>();
        columns = columns.stream().map(c -> {
            c.init();
            if(primary.equals(c.getColumnKey())){
                key = c;
            }else {
                normalColumns.add(c);
            }
            return c;
        }).collect(Collectors.toList());
        importNames = columns.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(o -> o.getJavaType()))),
                        ArrayList::new));

    }

    /** 拼接路径 **/
    public String buildPath(String path) {
        return basePath + String.format(path, packagePath);
    }

    /** 首字母转小写 **/
    public static String toLowerCaseFirstOne(String s) {
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /** 首字母转大写 **/
    public static String toUpperCaseFirstOne(String s) {
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }
}
